package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.domain.order.cqrs.OrderPageQuery;
import com.starlight.flower_server.infrastructure.entity.Order;
import com.starlight.flower_server.infrastructure.mapper.OrderMapper;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import com.starlight.flower_server.infrastructure.util.PageUtils;
import com.starlight.flower_server.infrastructure.repository.OrderRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author yfy2001
 * @date 2023/4/18 8:07
 */
@Repository
public class OrderRepositoryImpl implements OrderRepository {

    @Resource
    private OrderMapper orderMapper;


    @Override
    public Order queryDetail(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public Integer logicDelete(Long id) {
        return orderMapper.deleteById(id);
    }

    @Override
    public Integer insert(Order order) {
        String operator= CurrentUserInfoUtils.get("username");
        order.setCreator(operator);
        order.setModifier(operator);
        order.setCreated(new Date());
        order.setModified(new Date());
        order.setIsDeleted(0);
        //创建时为未支付未完成
        order.setIsPaid(0);
        order.setIsCompleted(0);
        return orderMapper.insert(order);
    }

    @Override
    public Integer modify(Order order) {
        String modifier= CurrentUserInfoUtils.get("username");
        order.setModifier(modifier);
        order.setModified(new Date());
        return orderMapper.updateById(order);
    }

    @Override
    public boolean exists(Order order) {
        return orderMapper.exists(new LambdaQueryWrapper<Order>()
                .eq(Order::getItemId,order.getItemId())
                .eq(Order::getIsPaid,0)
                .or()
                .eq(Order::getItemId,order.getItemId())
                .eq(Order::getIsCompleted,0));
    }

    @Override
    public IPage<Order> queryPage(OrderPageQuery query) {
        QueryWrapper<Order> queryWrapper= PageUtils.createOrderQuery(query);
        queryWrapper.lambda()
                .isNotNull(Order::getId)
                .eq(Objects.nonNull(query.getId()), Order::getId,query.getId())
                .eq(Objects.nonNull(query.getItemId()), Order::getItemId,query.getItemId())
                .eq(Objects.nonNull(query.getIsPaid()),Order::getIsPaid,query.getIsPaid())
                .eq(Objects.nonNull(query.getIsCompleted()),Order::getIsCompleted,query.getIsCompleted());
        return orderMapper.selectPage(PageUtils.startPage(query),queryWrapper);
    }

    @Override
    public List<Order> queryListByFlowerUserId(Long flowerUserId) {
        return orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .eq(Order::getFlowerUserId,flowerUserId));
    }

    @Override
    public List<Order> queryListByIdList(List<Long> orderIdList) {
        return orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .in(Order::getId,orderIdList));
    }

    @Override
    public List<Order> queryUnaccomplishedListByFlowerUserId(Long flowerUserId) {
        return orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .eq(Order::getFlowerUserId,flowerUserId)
                .eq(Order::getIsCompleted,0));
    }

    @Override
    public List<Order> queryAll() {
        return orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .isNotNull(Order::getId));
    }

    @Override
    public Long queryTotalCount() {
        return orderMapper.selectCount(new LambdaQueryWrapper<Order>()
                .isNotNull(Order::getId));
    }
}
